﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Products;
using MinhTuan;
public partial class admin_product_EditProduct : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
            String[] admin = Session["Admin"].ToString().Split(',');
            int id = int.Parse(admin[0].ToString());
            if (id < 0) Response.Redirect("http://localhost:2011/didong/admin/login.aspx");
            if (!Page.IsPostBack)
            {
                Binhdata();
                if (Request.QueryString["id"] != null)
                {
                    showproduct();
                }
          
            }   
    }
    protected void Binhdata()
    {
        this.Dropcategory.DataSource = Category.GetCategory();
        this.Dropcategory.DataBind();
        this.Dropbrand.DataSource = Brand.GetBrand();
        this.Dropbrand.DataBind();
        this.drlType.DataSource = Class.GetClass();
        this.drlType.DataBind();
    }
    protected void showproduct()
    {
        int id =int.Parse(Request.QueryString["id"].ToString());
       Product myproduct = Product.GetProductdetail(id);
        if (myproduct.Products_Id > 0)
        {
            hdid.Value = myproduct.Products_Id.ToString();
            Dropcategory.SelectedValue = myproduct.Category_Id.ToString();
            Dropbrand.SelectedValue = myproduct.Brand_Id.ToString();
            txtname.Text = myproduct.Products_Name;
            txtprice.Text = myproduct.Products_Price.ToString();
            txtquantity.Text = myproduct.Products_Quantity.ToString();
            txtunit.Text = myproduct.Products_Unit;
            if(myproduct.Products_Detail!="")CKEditor1.Text = myproduct.Products_Detail;
            txtwaranry.Text = myproduct.Products_Warranty;
            txtquantity.Text = myproduct.Products_Quantity.ToString();
            this.ckhighlights.Checked = myproduct.Ishot;
            this.cknews.Checked = myproduct.IsNew;
            txttitleimage.Text = myproduct.Products_DescriptionImage;
            imgPic.ImageUrl = "http://localhost:2011/didong/" + myproduct.Products_Image;
            hfImage.Value = myproduct.Products_Image.ToString();
            
            
        }
    }
    protected void Dropcategory_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void BtnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("http://localhost:2011/didong/admin/product");
    }
    protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            Product myproduct = new Product();
            myproduct.Products_Id = int.Parse(hdid.Value.ToString());
            myproduct.Products_Name = txtname.Text;
            myproduct.Brand_Id = int.Parse(Dropbrand.SelectedValue.ToString());
            myproduct.Category_Id = int.Parse(Dropcategory.SelectedValue.ToString());
            myproduct.Class_Id = int.Parse(drlType.SelectedValue.ToString());
            myproduct.Products_Price = Convert.ToInt32(txtprice.Text);
            myproduct.Products_Quantity = int.Parse(txtquantity.Text);
            myproduct.Products_Title = Access.RemoveVietnameseSigns(txtname.Text);
            myproduct.Products_Unit = txtunit.Text;
            if (fuImage.HasFile) myproduct.Products_Image = "image/product/" + fuImage.FileName;
            else myproduct.Products_Image = hfImage.Value.ToString();
            myproduct.Products_Detail = CKEditor1.Text;
            myproduct.Ishot = ckhighlights.Checked;
            myproduct.IsNew = cknews.Checked;
            myproduct.Products_Warranty = txtwaranry.Text;
            myproduct.Products_Origin = "Hang moi";
            myproduct.Products_DescriptionImage = txtname.Text;
            myproduct.Products_DateUpdate = DateTime.Now;
            myproduct.IsActive = true;
            Boolean Result = Product.Update_Product(myproduct);
            if (Result == true)
            {
                Lmessage.Text = "<strong>Cập nhật thành công</strong>";
            }
            else Lmessage.Text = "<strong>Cập nhật không thành công</strong>";

        }
        
      
    }
    protected void CKEditor1_TextChanged(object sender, EventArgs e)
    {
        
    }
}
